﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using MatthiWare.PortChecker.Events;

namespace MatthiWare.PortChecker.UI
{
    public partial class CheckPortDialog : Form
    {
        public PortCollection PortCollection { get; set; }
        public CheckPortDialog(PortCollection pc)
        {
            InitializeComponent();
            PortCollection = pc;
        }

        public void SetIp(String ip)
        {
            txtIp.Text = ip;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            UInt16 port;
            IPAddress ip;

            bool suc1 = UInt16.TryParse(txtPort.Text, out port);
            bool suc2 = IPAddress.TryParse(txtIp.Text, out ip);

            if (!suc1 && !suc2)
            {
                MessageBox.Show("Invalid ip and port", "Invalid cast error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (!suc1)
            {
                MessageBox.Show("Invalid port", "Invalid cast error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (!suc2)
            {
                MessageBox.Show("Invalid ip", "Invalid cast error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SetButtonEnabled(false);

            CheckPortTask task = new CheckPortTask(new EventHandler<PortEventArgs>((Object o, PortEventArgs ee) =>
            {
                PortInfo pInfo = new PortInfo(PortCollection.GetPortInfo(ee.Port), ee.Open, ee.Message);

                SetButtonEnabled(true);

                if (ee.Open)
                    MessageBox.Show(String.Format("The port {0} from IP {1} is open.\nName: {2}\nDescription: {3}\nMessage: {4}", ee.Port.ToString(), txtIp.Text, pInfo.Name, pInfo.Description, pInfo.Message), "Port " + pInfo.PortNumber + " is open!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                else
                    MessageBox.Show(String.Format("The port {0} from IP {1} is closed.\nName: {2}\nDescription: {3}\nMessage: {4}", ee.Port.ToString(), txtIp.Text, pInfo.Name, pInfo.Description, pInfo.Message), "Port " + pInfo.PortNumber + " is closed!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }), ip);

            task.BeginCheckPort(port);
        }

        delegate void ButtonDelegate(bool e);
        private void SetButtonEnabled(bool p)
        {
            if (btnCheck.InvokeRequired)
            {
                ButtonDelegate dlg = new ButtonDelegate(SetButtonEnabled);
                btnCheck.Invoke(dlg, p);
            }
            else
            {
                btnCheck.Enabled = p;
            }
        }

        private void txtIp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtPort.Focus();
        }

        private void txtPort_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnCheck.PerformClick();
        }
    }
}
